package mysql

import (
	"dinling/dinlingrpc/dinlingrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Title string  `gorm:"type:varchar(30);comment:标题"`
	Name  string  `gorm:"type:varchar(30);comment:商品名称"`
	Price float64 `gorm:"type:decimal(10,2);comment:商品价格"`
	Typer int64   `gorm:"type:int;comment:商品类型"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}
func GoodsList(db *gorm.DB, in *dinlingrpc.GoodsListRequest) ([]*dinlingrpc.GoodsList, error) {
	//SELECT
	//  goods.id,
	//  goods.title,
	//  goods.`name`,
	//  goods.price,
	//  typers.typer_name
	//FROM
	//  goods
	//  LEFT JOIN typers on goods.typer = typers.id
	var list []*dinlingrpc.GoodsList
	err2 := db.Debug().Model(&Goods{}).Select(
		"goods.id as goods_id",
		"goods.title",
		"goods.`name`",
		"goods.price",
		"typers.typer_name",
	).Joins("LEFT JOIN typers on goods.typer = typers.id").
		Where("goods.id=?", in.GoodsId).
		Find(&list).Error
	return list, err2
}
